#include <iostream>
#include "ObjectManager.hpp"
#include <memory>


using namespace HandleSystem;

// 测试对象
class TestObject1 {
public:
	TestObject1() { std::cout << "TestObject1构造对象" << std::endl; }
	~TestObject1() { std::cout << "TestObject1析构对象" << std::endl; }
	int x;
	void PrintValue() { std::cout << "TestObject1对象值: " << x << std::endl; }
};
// 测试对象
class TestObject2 {
public:
	TestObject2() { std::cout << "TestObject2构造对象" << std::endl; }
	~TestObject2() { std::cout << "TestObject2析构对象" << std::endl; }
	int x;
	void PrintValue() { std::cout << "TestObject2对象值: " << x << std::endl; }
};

// 全局环境变量
auto globalEnv = new HandleManagerEnv;


int main() {
	std::cout << "===== 开始对象句柄管理系统测试 =====" << std::endl;

	// 基本功能测试

	ObjectHandleManager<TestObject1> manager1(globalEnv);
	ObjectHandleManager<TestObject2> manager2(globalEnv);
	// 创建新对象句柄
	ObjectHandle handle1 = manager1.CreateObject("", "TestObject1");
	std::cout << "主线程: 创建匿名对象句柄 = " << handle1 << std::endl;
	ObjectHandle handle2 = manager2.CreateObject("", "TestObject2");
	std::cout << "主线程: 创建匿名对象句柄 = " << handle2 << std::endl;

	Object<TestObject1> obj1 = manager1.GetObject(handle1);
	std::shared_ptr<TestObject1> objRef1 = obj1.SharedPtr();
	objRef1->x = 100;
	objRef1->PrintValue();
	Object<TestObject2> obj2 = manager2.GetObject(handle2);
	std::shared_ptr<TestObject2> objRef2 = obj2.SharedPtr();
	objRef2->x = 200;
	objRef2->PrintValue();
	// 释放句柄并检查对象销毁
	std::cout << "TestObject1句柄数：" << obj1.HandleCount() << std::endl;
	manager1.CloseHandle(handle1);
	std::cout << "TestObject2句柄数：" << obj2.HandleCount() << std::endl;
	manager2.CloseHandle(handle2);
	std::cout << "TestObject1句柄数：" << obj1.HandleCount() << std::endl;
	std::cout << "TestObject2句柄数：" << obj2.HandleCount() << std::endl;
	std::cout << "主线程: 释放句柄" << std::endl;
	// 清理环境
	delete globalEnv;

	std::cout << "\n===== 测试完成 =====" << std::endl;
	return 0;
}